package com.mike.uaa.server.authentication.token;


import com.mike.uaa.core.AbstractAuthenticationToken;
import com.mike.uaa.core.GrantedAuthority;

import java.util.Collection;

/**
 * @author z zhang
 */
public class UsernamePasswordAuthenticationToken extends AbstractAuthenticationToken {

    private static final long serialVersionUID = 5517018814206006638L;

    private final Object principal;

    private Object credential;

    public UsernamePasswordAuthenticationToken(Object principal, Object credential) {
        super(null);
        this.principal = principal;
        this.credential = credential;
        setAuthenticated(false);
    }

    public UsernamePasswordAuthenticationToken(Object principal, Object credential,
                                               Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        this.credential = credential;
        setAuthenticated(true);
    }

    public static UsernamePasswordAuthenticationToken unauthenticated(Object principal, Object credential) {
        return new UsernamePasswordAuthenticationToken(principal, credential);
    }

    public static UsernamePasswordAuthenticationToken authenticated(Object principal, Object credential,
                                                                    Collection<? extends GrantedAuthority> authorities) {
        return new UsernamePasswordAuthenticationToken(principal, credential, authorities);
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    @Override
    public Object getCredential() {
        return this.credential;
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
        this.credential = null;
    }
}
